package com.example.lifecycle.app;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentCallbacks;
import android.content.ComponentCallbacks2;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

import com.example.lifecycle.MainActivity;

/**
 * 生命周期管理器.
 */

public class LifecycleActivityListener implements Application.ActivityLifecycleCallbacks {

    public static void init(Application application) {
        application.registerActivityLifecycleCallbacks(new LifecycleActivityListener());
        application.registerComponentCallbacks(new ComponentCallbacks2() {
            @Override
            public void onTrimMemory(int i) {

            }

            //当设备的信息改变的时候将会调用,某些时候你需要在系统一些配置信息改变的时候activity避免重新onCreate(),
            // 你就可以在android:configChanges声明，某个属性已经声明了，
            // 那么在这个属性的信息改变的时候就只会回调onConfigurationChanged(Configuration newConfig)方法
            @Override
            public void onConfigurationChanged(Configuration configuration) {

            }

            //在系统内存不足，所有后台程序（优先级为background的进程，不是指后台运行的进程）都被杀死时，系统会调用OnLowMemory.
            @Override
            public void onLowMemory() {

            }
        });
    }

    public LifecycleActivityListener() {
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
        if (activity instanceof MainActivity) {
//                    DaggerAppComponent.builder().build().inject((MainActivity) activity);
        }

        if (activity instanceof FragmentActivity) {
            ((FragmentActivity) activity).getSupportFragmentManager()
                    .registerFragmentLifecycleCallbacks(
                            new FragmentManager.FragmentLifecycleCallbacks() {
                                @Override
                                public void onFragmentCreated(FragmentManager fm, Fragment f,
                                                              Bundle savedInstanceState) {

                                }

                                @Override
                                public void onFragmentResumed(FragmentManager fm, Fragment f) {
                                    super.onFragmentResumed(fm, f);
                                }

                            }, true);
        }


        System.out.println("####Application " + activity.getClass().getSimpleName() + " onActivityCreated");
    }

    @Override
    public void onActivityStarted(Activity activity) {
        System.out.println("####Application " + activity.getClass().getSimpleName() + " onActivityStarted");

    }

    @Override
    public void onActivityResumed(Activity activity) {
        System.out.println("####Application " + activity.getClass().getSimpleName() + " onActivityResumed");

    }

    @Override
    public void onActivityPaused(Activity activity) {
        System.out.println("####Application " + activity.getClass().getSimpleName() + " onActivityPaused");

    }

    @Override
    public void onActivityStopped(Activity activity) {
        System.out.println("####Application " + activity.getClass().getSimpleName() + " onActivityStopped");

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
        System.out.println("####Application " + activity.getClass().getSimpleName() + " onActivitySaveInstanceState");

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        System.out.println("####Application " + activity.getClass().getSimpleName() + " onActivityDestroyed");
    }


}
